package be.kuleuven.cs.mume.objects;

import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.World;
import org.mt4j.components.TransformSpace;
import org.mt4j.components.visibleComponents.font.FontManager;
import org.mt4j.components.visibleComponents.font.IFont;
import org.mt4j.components.visibleComponents.shapes.AbstractShape;
import org.mt4j.components.visibleComponents.shapes.MTLine;
import org.mt4j.components.visibleComponents.shapes.MTPolygon;
import org.mt4j.components.visibleComponents.shapes.MTRectangle;
import org.mt4j.components.visibleComponents.shapes.MTRoundRectangle;
import org.mt4j.components.visibleComponents.widgets.MTTextArea;
import org.mt4j.util.MTColor;
import org.mt4j.util.animation.Animation;
import org.mt4j.util.animation.MultiPurposeInterpolator;
import org.mt4j.util.math.Ray;
import org.mt4j.util.math.Vector3D;
import org.mt4j.util.math.Vertex;

import processing.core.PApplet;
import processing.core.PGraphics;
import advanced.physics.physicsShapes.IPhysicsComponent;
import advanced.physics.physicsShapes.PhysicsRectangle;
import advanced.physics.util.PhysicsHelper;
import be.kuleuven.cs.mume.animations.LineRotationAnimationListener;

public class HelloWorldObject extends AbstractShape implements IPhysicsComponent{
	
	public final PhysicsBox rect;
	protected PApplet applet;
	
	public HelloWorldObject(PApplet applet, float x, float y , World world, float scale){
		super(new Vertex[0], applet);
		this.applet = applet;
		final MTColor clearColor = new MTColor(146, 150, 188, 255);
		MTColor white = new MTColor(255,255,255);

		IFont fontArial = FontManager.getInstance().createFont(applet, "arial.ttf", 
				50, 	//Font size
				white,  //Font fill color
				white);	//Font outline color
		
		final MTTextArea textField = new MTTextArea(applet, fontArial); 

		textField.setNoStroke(true);
		textField.setNoFill(true);
		textField.setPickable(false); //IMPORTANT, DO THIS FOR EVERY SUB-OBJECT

		textField.setText("Hello World!");

		//new MTRoundRectangle(x,y, 0, textField.getWidthXY(TransformSpace.GLOBAL), textField.getHeightXY(TransformSpace.GLOBAL), 10, 10, applet);
		rect = new PhysicsBox(new Vector3D(x,y), textField.getWidthXY(TransformSpace.GLOBAL), textField.getHeightXY(TransformSpace.GLOBAL), applet, world, 1f, 0.4f, 1.0f, scale);
		//PhysicsHelper.addDragJoint(world, rect, rect.getBody().isDynamic(), scale);
		this.addChild(rect);
		rect.addChild(textField);
		rect.setFillColor(clearColor);
		rect.setPickable(false);//IMPORTANT, DO THIS FOR EVERY SUB-OBJECT
		textField.setPositionGlobal(rect.getCenterPointGlobal());

		//addCrosshair();
	}

	private void addCrosshair() {
		MTLine line1 = new MTLine(applet, new Vertex(0, 10), new Vertex(0, -10));
		rect.addChild(line1);
		Vector3D anchorpoint = rect.getCenterPointGlobal();
		anchorpoint.translate(new Vector3D(-rect.getWidthXY(TransformSpace.GLOBAL)/2,-rect.getHeightXY(TransformSpace.GLOBAL)/2,0));
		line1.setPositionRelativeToParent(anchorpoint);
		line1.setPickable(false);//IMPORTANT, DO THIS FOR EVERY SUB-OBJECT
		Animation anim = new Animation("rotate", new MultiPurposeInterpolator( 0,360, 4000, 0, 1f, -1), line1);
		anim.addAnimationListener(new LineRotationAnimationListener());
		anim.start();
		
		MTLine line2 = new MTLine(applet, new Vertex(10,0), new Vertex(-10,0));
		rect.addChild(line2);
		line2.setPositionRelativeToParent(anchorpoint);
		line2.setPickable(false);//IMPORTANT, DO THIS FOR EVERY SUB-OBJECT
		anim = new Animation("rotate", new MultiPurposeInterpolator( 0,360, 4000, 0, 1f, -1), line2);
		anim.addAnimationListener(new LineRotationAnimationListener());
		anim.start();
	}

	@Override
	protected void destroyComponent() {
		rect.destroy();
		
	}

	@Override
	public Vector3D getCenterPointLocal() {
		return rect.getCenterPointRelativeToParent();
	}

	@Override
	public Vector3D getGeometryIntersectionLocal(Ray ray) {
		return rect.getGeometryIntersectionLocal(ray);
	}

	@Override
	public boolean isGeometryContainsPointLocal(Vector3D testPoint) {
		return rect.isGeometryContainsPointLocal(testPoint);
	}

	@Override
	public void drawComponent(PGraphics g) {
		rect.drawComponent(g);
		
	}

	public MTRectangle getRect() {
		return rect;
	}

	@Override
	public Body getBody() {
		return rect.getBody();
	}

	@Override
	public void setCenterRotation(float angle) {
		rect.setCenterRotation(angle);
		
	}

}
